select  ft.ftz_num                                                                       NRO_FATTURAZIONE,   
        ft.FAT_ANNO||'-'||ft.FAT_NUM                                             NRO_FATTURA,
                                                                         
       ft.tfat_id                                                                        ID_FATTURA,
        vl.VALFL_VALDIS                                                                    TIPO_FATTURA,

        ft.regivapro_label                                                                LABEL_PROTOCOLLO_IVA,
     --   ft.ragf_id                                                                                        ID_RAGGRUPPAMENTO,
        trunc( ft.FAT_DT)                                                               DATA_FATTURA,
        rf.sog_id                                                                         CODICE_CLIENTE,
        NVL( sog.sog_codfis, sog.SOG_PARIVA )             CODICE_FISCALE_PARTITA_IVA,
        ft.FAT_DECRAGSOC                                                                  RAGIONE_SOCIALE_CLIENTE,
       cuf.con_anno||'/'||cuf.con_id                               contratto,
       -- cuf.con_id                                                                        
        cont.con_oldconid                                                                 VECCHIO_CODICE_CONTRATTO,

        cuf.att_idext  POD,
        ( select max(vatt.CCON_VALVIS)
            from  v_valcxcuf  vatt
            where cuf.az_id   = vatt.az_id
              and vatt.sta_idsys = 2 
              and cuf.cuf_id = vatt.cuf_id
              and vatt.ccon_id = 27
              and vatt.ver_dtini <= s.sfat_dtfinevalcon
              and vatt.ver_dtfine >= s.sfat_dtinivalcon  
              )                                                                                                              TIPO_FORNITURA,
              
           ( select max(VATT.VCATT_VAL )
            from  v_valcxatt  vatt
            where cuf.az_id   = vatt.az_id
              and vatt.sta_idsys = 2 
              and cuf.att_id = vatt.att_id
              and vatt.catt_id = 3
              
              )                                                                                                         POT_IMPEGNATA,
       
        ( select min(trunc( cufrip.ver_dtini)) 
            from  v_conubiperrip  cufrip,
                  fatturasrip   fsrip
            where fsrip.az_id   = ft.az_id
              and fsrip.sfat_id = s.sfat_id
              and cufrip.az_id  = ft.az_id
--              and cufrip.sta_idsys = 2 
              and cufrip.cufpr_num = fsrip.cufpr_num )                                    DATA_INI_PERIODO_FATTURATO,
        ( select max(trunc( cufrip.ver_dtfine) )
            from  v_conubiperrip  cufrip,
                  fatturasrip   fsrip
            where fsrip.az_id   = ft.az_id
              and fsrip.sfat_id = s.sfat_id
              and cufrip.az_id  = ft.az_id
--              and cufrip.sta_idsys = 2 
              and cufrip.cufpr_num = fsrip.cufpr_num )                                    DATA_FINE_PERIODO_FATTURATO,
        ( select max(( trunc( cufrip.ver_dtfine ) - trunc( cufrip.ver_dtini )) + 1)
            from  v_conubiperrip  cufrip,
                  fatturasrip   fsrip
            where fsrip.az_id   = ft.az_id
              and fsrip.sfat_id = s.sfat_id
              and cufrip.az_id  = ft.az_id
--              and cufrip.sta_idsys = 2
              and cufrip.cufpr_num = fsrip.cufpr_num )                                    GG_FATTURATI,
      ( select min (trunc(con.ver_dtini )) 
            from  fatturaslc   f,
                     convalsf sf,
                     conval con 
            where f.az_id   = ft.az_id
              and f.sfat_id = s.sfat_id
              and sf.az_id = f.az_id  
             and f.conssf_id = sf.conssf_id
              and sf.az_id  = con.az_id
              and sf.cval_vernum = con.ver_num 
             --and cons.sta_idsys in (3,10) 
              and f.sfatlc_fltipoope = '0')                                            DATA_INI_CONSUMO_FATTURATO,
             
        ( select max ( trunc( con.ver_dtfine)  )
         from  fatturaslc   f,
                     convalsf sf,
                     conval con 
            where f.az_id   = ft.az_id
              and f.sfat_id = s.sfat_id
              and sf.az_id = f.az_id  
             and f.conssf_id = sf.conssf_id
              and sf.az_id  = con.az_id
              and sf.cval_vernum = con.ver_num 
             --and cons.sta_idsys in (3,10) 
              and f.sfatlc_fltipoope = '0')                                            DATA_FINE_CONSUMO_FATTURATO,         
        cuf.DECODE_INDID                                                                  INDIRIZZO_FORNITURA,
    --    cuf.COM_DES                                                                       COMUNE_FORNITURA,
  --      s.sfat_id,
         ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'FU'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon in ( '1','2') )                                        ATT_FU,
         ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'F1'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon  in ( '1','2') )                                        ATT_F1,
         ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'F2'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon  in ( '1','2'))                                        ATT_F2,
         ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'F3'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon  in ( '1','2') )                                        ATT_F3,
     

      ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'FU'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon in ('1','2') )                                        ATT_FU_RETTIFICA,
      ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'F1'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon in ('1','2') )                                        ATT_F1_RETTIFICA,
      ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'F2'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon in ('1','2') )                                        ATT_F2_RETTIFICA,
      ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'F3'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon in ('1','2'))                                        ATT_F3_RETTIFICA,
          
          
                  ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'FU'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon in ( '1','2') )                                        REA_FU,
         ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'F1'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon  in ( '1','2') )                                        REA_F1,
         ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'F2'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon  in ( '1','2'))                                        REA_F2,
         ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'F3'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon  in ( '1','2') )                                        REA_F3,
     

      ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'FU'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon in ('1','2') )                                        REA_FU_RETTIFICA,
      ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'F1'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon in ('1','2') )                                        REA_F1_RETTIFICA,
      ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'F2'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon in ('1','2') )                                        REA_F2_RETTIFICA,
      ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'F3'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon in ('1','2'))                                        REA_F3_RETTIFICA,
                   ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'FU'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon in ( '1','2') )                                        POT_FU,
         ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'F1'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon  in ( '1','2') )                                        POT_F1,
         ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'F2'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon  in ( '1','2'))                                        POT_F2,
         ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'F3'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon  in ( '1','2') )                                        POT_F3,
     

      ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'FU'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon in ('1','2') )                                        POT_FU_RETTIFICA,
      ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'F1'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon in ('1','2') )                                        POT_F1_RETTIFICA,
      ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'F2'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon in ('1','2') )                                        POT_F2_RETTIFICA,
      ( select sum(c.cval_val)
          from 
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = s.sfat_id
          and s.az_id = s.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'F3'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon in ('1','2'))                                        POT_F3_RETTIFICA,
   
                           
        ( select o.ot_dessta
           from   v_cufot  cu,
                  ot       o
           where cu.az_id = s.az_id
           and cu.cuf_id = s.cuf_id
           and cu.sta_idsys = 2
           and cu.ver_dtini <= s.sfat_dtfinevalcon
           and cu.ver_dtfine >= s.sfat_dtinivalcon
           and cu.ver_dtini = (  select max(ver_dtini) 
                                 from v_cufot 
                                 where az_id = cu.az_id
                                 and cuf_id = cu.cuf_id 
                                 and ctu_numcom = cu.ctu_numcom
                                 and sta_idsys =  cu.sta_idsys
                                 and ver_dtini <= s.sfat_dtfinevalcon
                                 and ver_dtfine >= s.sfat_dtinivalcon  )
           and o.az_id = cu.az_id
           and o.ot_id = cu.ot_id
           and o.ser_id = '03'
           and o.catcomser_id = 7
           and o.comser_id = 16
            )                                                              OPZIONE_VENDITA,
        ( select o.ot_dessta
           from   v_cufot  cu,
                  ot       o
           where cu.az_id = s.az_id
           and cu.cuf_id = s.cuf_id
           and cu.sta_idsys = 2
           and cu.ver_dtini <= s.sfat_dtfinevalcon
           and cu.ver_dtfine >= s.sfat_dtinivalcon
           and cu.ver_dtini = (  select max(ver_dtini) 
                                 from v_cufot 
                                 where az_id = cu.az_id
                                 and cuf_id = cu.cuf_id 
                                 and ctu_numcom = cu.ctu_numcom
                                 and sta_idsys =  cu.sta_idsys
                                 and ver_dtini <= s.sfat_dtfinevalcon
                                 and ver_dtfine >= s.sfat_dtinivalcon  )
           and o.az_id = cu.az_id
           and o.ot_id = cu.ot_id
           and o.ser_id = '03'
           and o.catcomser_id = 7
           and o.comser_id = 17
            )                                                              OPZIONE_ERARIALE,
        ( select o.ot_dessta
           from   v_cufot  cu,
                  ot       o
           where cu.az_id = s.az_id
           and cu.cuf_id = s.cuf_id
           and cu.sta_idsys = 2
           and cu.ver_dtini <= s.sfat_dtfinevalcon
           and cu.ver_dtfine >= s.sfat_dtinivalcon
           and cu.ver_dtini = (  select max(ver_dtini) 
                                 from v_cufot 
                                 where az_id = cu.az_id
                                 and cuf_id = cu.cuf_id 
                                 and ctu_numcom = cu.ctu_numcom
                                 and sta_idsys =  cu.sta_idsys
                                 and ver_dtini <= s.sfat_dtfinevalcon
                                 and ver_dtfine >= s.sfat_dtinivalcon  )
           and o.az_id = cu.az_id
           and o.ot_id = cu.ot_id
           and o.ser_id = '03'
           and o.catcomser_id = 7
           and o.comser_id = 18
            )                                                              OPZIONE_ADDIZIONALE_PROV,
        ( select o.ot_dessta
           from   v_cufot  cu,
                  ot       o
           where cu.az_id = s.az_id
           and cu.cuf_id = s.cuf_id
           and cu.sta_idsys = 2
           and cu.ver_dtini <= s.sfat_dtfinevalcon
           and cu.ver_dtfine >= s.sfat_dtinivalcon
           and cu.ver_dtini = (  select max(ver_dtini) 
                                 from v_cufot 
                                 where az_id = cu.az_id
                                 and cuf_id = cu.cuf_id 
                                 and ctu_numcom = cu.ctu_numcom
                                 and sta_idsys =  cu.sta_idsys
                                 and ver_dtini <= s.sfat_dtfinevalcon
                                 and ver_dtfine >= s.sfat_dtinivalcon  )
           and o.az_id = cu.az_id
           and o.ot_id = cu.ot_id
           and o.ser_id = '03'
           and o.catcomser_id = 7
           and o.comser_id = 19
            )                                                             OPZIONE_ADDIZIONALE_COMUN,           
                ( select o.ot_dessta
           from   v_cufot  cu,
                  ot       o
           where cu.az_id = s.az_id
           and cu.cuf_id = s.cuf_id
           and cu.sta_idsys = 2
           and cu.ver_dtini <= s.sfat_dtfinevalcon
           and cu.ver_dtfine >= s.sfat_dtinivalcon
           and cu.ver_dtini = (  select max(ver_dtini) 
                                 from v_cufot 
                                 where az_id = cu.az_id
                                 and cuf_id = cu.cuf_id 
                                 and ctu_numcom = cu.ctu_numcom
                                 and sta_idsys =  cu.sta_idsys
                                 and ver_dtini <= s.sfat_dtfinevalcon
                                 and ver_dtfine >= s.sfat_dtinivalcon  )
           and o.az_id = cu.az_id
           and o.ot_id = cu.ot_id
           and o.ser_id = '03'
           and o.catcomser_id = 8
           and o.comser_id = 21
            )                                                              OPZIONE_TRASPORTO,
        ( select o.ot_dessta
           from   v_cufot  cu,
                  ot       o
           where cu.az_id = s.az_id
           and cu.cuf_id = s.cuf_id
           and cu.sta_idsys = 2
           and cu.ver_dtini <= s.sfat_dtfinevalcon
           and cu.ver_dtfine >= s.sfat_dtinivalcon
           and cu.ver_dtini = (  select max(ver_dtini) 
                                 from v_cufot 
                                 where az_id = cu.az_id
                                 and cuf_id = cu.cuf_id 
                                 and ctu_numcom = cu.ctu_numcom
                                 and sta_idsys =  cu.sta_idsys
                                 and ver_dtini <= s.sfat_dtfinevalcon
                                 and ver_dtfine >= s.sfat_dtinivalcon  )
           and o.az_id = cu.az_id
           and o.ot_id = cu.ot_id
           and o.ser_id = '03'
           and o.catcomser_id = 7
           and o.comser_id = 22
            )                                                               OPZIONE_TRASMISSIONE,

        ( select o.ot_dessta
           from   v_cufot  cu,
                  ot       o
           where cu.az_id = s.az_id
           and cu.cuf_id = s.cuf_id
           and cu.sta_idsys = 2
           and cu.ver_dtini <= s.sfat_dtfinevalcon
           and cu.ver_dtfine >= s.sfat_dtinivalcon
           and cu.ver_dtini = (  select max(ver_dtini) 
                                 from v_cufot 
                                 where az_id = cu.az_id
                                 and cuf_id = cu.cuf_id 
                                 and ctu_numcom = cu.ctu_numcom
                                 and sta_idsys =  cu.sta_idsys
                                 and ver_dtini <= s.sfat_dtfinevalcon
                                 and ver_dtfine >= s.sfat_dtinivalcon  )
           and o.az_id = cu.az_id
           and o.ot_id = cu.ot_id
           and o.ser_id = '03'
           and o.catcomser_id = 8
           and o.comser_id = 24
            )                                                              OPZIONE_PENALE_REATTIVA,      

         
            
 ( SELECT SUM(FATVOCEOT_IMP) FROM UE_V_FATTURAR AR WHERE    AR.AZ_ID ='02' AND s.SFAT_ID=AR.SFAT_ID AND AR.TFAT_ID=FT.TFAT_ID  AND OT_ID = 517 AND VOCE_ID=250 ) QUOTA_VARIABILE_DOM,    
 ( SELECT SUM(FATVOCEOT_IMP) FROM UE_V_FATTURAR AR WHERE    AR.AZ_ID ='02' AND s.SFAT_ID=AR.SFAT_ID AND AR.TFAT_ID=FT.TFAT_ID  AND OT_ID <> 517 AND VOCE_ID=250 ) QUOTA_VARIABILE_AU,  
(SELECT SUM(FATVOCEOT_IMP) FROM UE_V_FATTURAR AR WHERE    AR.AZ_ID ='02' AND s.SFAT_ID=AR.SFAT_ID AND AR.TFAT_ID=FT.TFAT_ID  AND OT_ID IN ( 517,518) AND VOCE_ID=251 ) QUOTA_FISSA_DOM,
(SELECT SUM(FATVOCEOT_IMP) FROM UE_V_FATTURAR AR WHERE    AR.AZ_ID ='02' AND s.SFAT_ID=AR.SFAT_ID AND AR.TFAT_ID=FT.TFAT_ID  AND OT_ID <> 517 AND OT_ID <> 518 AND VOCE_ID=251 ) QUOTA_FISSA_AU,   
(SELECT SUM(FATVOCEOT_IMP) FROM UE_V_FATTURAR AR WHERE    AR.AZ_ID ='02' AND s.SFAT_ID=AR.SFAT_ID AND AR.TFAT_ID=FT.TFAT_ID  AND OT_ID IN ( 517,518) AND VOCE_ID=252 ) QUOTA_POT_DOM,
(SELECT SUM(FATVOCEOT_IMP) FROM UE_V_FATTURAR AR WHERE    AR.AZ_ID ='02' AND s.SFAT_ID=AR.SFAT_ID AND AR.TFAT_ID=FT.TFAT_ID  AND OT_ID <> 517 AND OT_ID <> 518 AND VOCE_ID=252 ) QUOTA_POT_AU,     
 BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, s.sfat_id, '248,283' )     A6_QUOTA_POTENZA, 
BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, s.sfat_id, '281' )                  TRASMISSIONE,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, s.sfat_id, '303,478')                  MISURA,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, s.sfat_id, '302,299,300' )          REATTIVA, 
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, s.sfat_id, '289,290,291,297' )      COMPONENTI_A_FISSE,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, s.sfat_id, '284,286,287,288,298,356' )   COMPONENTI_A, 
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, s.sfat_id, '295' )               COMPONENTI_UC_FISSE,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, s.sfat_id, '292,293,294,454' )       COMPONENTI_UC, 
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, s.sfat_id, '296' )               COMPONENTI_MCT,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, s.sfat_id, '234' )   PREZZO_VENDITA,  
       BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, s.sfat_id, '205' )               SBILANCIAMENTO_VENDITA,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, s.sfat_id, '6,14,15,21,22' )   PD,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, s.sfat_id, '469' )               UC1_PPE,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, s.sfat_id, '220,448,222,479' )               PCV_DISP,

        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, s.sfat_id, '242,401,532' )               IMPOSTA_ERARIALE,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, s.sfat_id, '470,471,472,473' )               PE,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, s.sfat_id, '245' )               IMPOSTA_ADDIZ_COMUNALE,
        


         (select sum(fatturar.FATVOCEOT_IMP ) from UE_V_FATTURAR fatturar where fatturar.tfat_id = ft.tfat_id and fatturar.voce_id in (25,304,
                  305,306,307,308,309,310,364,365,366,367,368,369,370,374,375,376,377,380,381,384,395,399,400,449,450,486,534,535)) LAVORI,
                    (select sum(fatturar.FATVOCEOT_IMP ) from UE_V_FATTURAR fatturar where fatturar.tfat_id = ft.tfat_id and fatturar.voce_id in (83,84)) imp_da_bollette_preced,
                     (select sum(fatturar.FATVOCEOT_IMP ) from UE_V_FATTURAR fatturar where fatturar.tfat_id = ft.tfat_id and fatturar.voce_id in (484,485,408,441)) BONUS,
                    (select sum(fatturar.FATVOCEOT_IMP ) from UE_V_FATTURAR fatturar where fatturar.tfat_id = ft.tfat_id and fatturar.voce_id in (9,10,16,17)) SCONTO_DIPEN,
       (select sum(fatturar.FATVOCEOT_IMP ) from UE_V_FATTURAR fatturar where fatturar.tfat_id = ft.tfat_id and fatturar.voce_id in (394,24,27,46,363)) interessi_mora, 
          (select sum(fatturar.FATVOCEOT_IMP ) from UE_V_FATTURAR fatturar where fatturar.tfat_id = ft.tfat_id and fatturar.voce_id in (515,516,517,518)) DEP_CAUZIONALE,
           (select sum(fatturar.FATVOCEOT_IMP ) from UE_V_FATTURAR fatturar where fatturar.tfat_id = ft.tfat_id and fatturar.voce_id in (529)) ARROTONDAMENTI,                    
        ( select sum(pfat_imp) 
         from fatturap 
         where tfat_id = ft.tfat_id
         and cuf_id = cuf.cuf_id )   TOT_IMPONIBILE_XCUF,
         ( select sum(pfat_iva) 
             from fatturap
            where tfat_id = ft.tfat_id
            and cuf_id = cuf.cuf_id )   TOT_IVA_XCUF,
         ( select sum(pfat_tot)
            from fatturap
            where tfat_id = ft.tfat_id
            and cuf_id = cuf.cuf_id )   TOT_FATTURA_XCUF,
      BO_FATTURAT.TOTALE_IMPONIBILE( ft.az_id, ft.tfat_id )                             TOT_IMPONIBILE_FATTURA,
      BO_FATTURAT.TOTALE_IVA( ft.az_id, ft.tfat_id )                                    TOT_IVA_FATTURA,        
      BO_FATTURAT.TOTALE_DA_PAGARE( ft.az_id, ft.tfat_id )                              TOT_FATTURA,            
      decode ( RFRP_FLTIPOPAG, 'D','RID - Domiciliazione Bancaria','B','Bonifico Bancario','R','RimessaDiretta','P','BollettinoPostale') TIPO_PAGAMENTO,
      banapp_abi Banca_appoggio,   
      decode ( RFRP_FLSTABOL, 0, 'No', 1, 'Si' )   STAMPA_BOLLETTINO_POSTALE

          
from  ragfat          rf,
    --  v_ragfatgrufat  rg,      
      soggetto        sog,
      UE_v_conubifrn     cuf,
      contratto       cont,
      fatturas        s,
      UE_v_fatturat      ft,
      valflag vl
where ft.az_id       = '02'
and cuf.SER_ID='03'
and vl.FLAG_ID= 'FAT_FLTIPO'
and vl.VALFL_VAL=ft.fat_fltipo
--and    ft.ftz_num    >6
and   rf.az_id       = ft.az_id
and   rf.ragf_id     = ft.ragf_id
--and   rg.az_id    = ft.az_id
--and   rg.ragf_id  = ft.ragf_id
--and   rg.STA_IDSYS = 2
--and   rg.sta_idsys(+) <> 3
and   sog.az_id      = ft.az_id
and   sog.sog_id     = rf.sog_id
and   s.az_id     = ft.az_id
AND s.SFAT_FLTIPO=1
and  s.tfat_id   = ft.tfat_id
and   cuf.az_id      = s.az_id(+)
and   cuf.cuf_id     = s.cuf_id(+)
and   cont.az_id     = ft.az_id
and   cont.con_anno  = cuf.con_anno
and   cont.con_id    = cuf.con_id
--and   cont.con_anno  = 2009
--and   cont.con_id    = 16607

--and ft.ftz_num =12483 
--AND FT.TFAT_ID=35233
order by fat_anno, fat_num, cuf.con_anno, cuf.con_id
